package com.dh.cheesestrip.Util;

import android.widget.EditText;
import android.widget.TextView;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;

/**
 * 字符串工具类
 *
 * @author M.c
 */
public class StringUtil {


    /**
     * 26英文字母字符串
     */
    public static String[] ENGLIST_LETTER = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

    /**
     * 判断字符串是否为空或者空字符串 如果字符串是空或空字符串则返回true，否则返回false。也可以使用Android自带的TextUtil
     *
     * @param str
     * @return
     */
    public static boolean isBlank(String str) {
        if (str == null || "".equals(str) || "null".equals(str)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否是中文
     *
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }



    /**
     * 将double转换为字符串，保留小数点位数
     *
     * @param doubleNum 需要解析的double
     * @param digitNum  小数点位数，小于0则默认0
     * @return
     */
    public static String DoubleToAmountString(Double doubleNum, int digitNum) {
        if (digitNum < 0)
            digitNum = 0;
        StringBuilder strBuilder = new StringBuilder("#");
        for (int i = 0; i < digitNum; i++) {
            if (i == 0)
                strBuilder.append(".#");
            else
                strBuilder.append("#");
        }
        DecimalFormat df = new DecimalFormat(strBuilder.toString());
        return df.format(doubleNum);
    }


    /**
     * 去除String中的某一个字符
     *
     * @param orgStr
     * @param splitStr 要去除的字符串
     * @return
     */
    public static String removeAllChar(String orgStr, String splitStr) {
        String[] strArray = orgStr.split(splitStr);
        String res = "";
        for (String tmp : strArray) {
            res += tmp;
        }
        return res;
    }

    /**
     * 获取非空edittext
     *
     * @param text
     * @return
     */
    public static String getEditText(EditText text) {
        if (null == text || text.getText().toString().trim().equals("")) {
            return "";
        }
        return text.getText().toString().trim();
    }

    /**
     * 获取非空textview
     *
     * @param text
     * @return
     */
    public static String getTextView(TextView text) {
        if (null == text || text.getText().toString().trim().equals("")) {
            return "";
        }
        return text.getText().toString().trim();
    }

    /**
     * MD5加密 32位小写
     *
     * @param sSecret
     * @return
     */
    public static String getMd5Value(String sSecret) {
        try {
            MessageDigest bmd5 = MessageDigest.getInstance("MD5");
            bmd5.update(sSecret.getBytes());
            int i;
            StringBuffer buf = new StringBuffer();
            byte[] b = bmd5.digest();
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * string转int
     * @param str
     * @return
     */
    public static int parseInt(String str){
        int i=0;

        try {
            i = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return i;
    }


    /**
     * string转double
     * @param str
     * @return
     */
    public static double parseDouble(String str){
        double i=0.0;

        try {
            i = Double.parseDouble(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        return i;
    }

}
